﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// Simple Service Administration Tool for WinNT based systems.
// Copyright © 2010 - Silvan Gehrig
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
// Author:
//  Silvan Gehrig
//
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SSATool.DL.Server.ConsoleApp.Properties;
using SSATool.Common.Util.Threading;
using SSATool.Common.Util.ExtensionMethods;

#endregion

namespace SSATool.DL.Server.ConsoleApp
{
    /// <summary>
    /// Starts the SSA Tool WCF services.
    /// </summary>
    class Program
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Application entry point for the console application.
        /// </summary>
        /// <param name="args"></param>
        public static void Main(string[] args)
        {
            AsyncProcess adminSvcProc = new AsyncProcess();
            AsyncProcess eventSvcProc = new AsyncProcess();
            ServiceAdminWcfServer adminSvcHost = new ServiceAdminWcfServer();
            ServiceEventWcfServer eventSvcHost = new ServiceEventWcfServer();

            try
            {
                Console.WriteLine(Resources.LogEventStartAdminService);
                adminSvcProc = new AsyncProcess();
                adminSvcProc.Finished += OnAdminSvcProcFinished;
                adminSvcProc.Run(adminSvcHost.Start);

                Console.WriteLine(Resources.LogEventStartEventService);
                eventSvcProc = new AsyncProcess();
                eventSvcProc.Finished += OnEventSvcProcFinished;
                eventSvcProc.Run(eventSvcHost.Start);

                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(Resources.LogEventOnError, e.ToFullErrorString());
            }
            finally
            {
                Console.WriteLine(Resources.LogEventOnStop);

                adminSvcProc.Finished -= OnAdminSvcProcFinished;
                eventSvcProc.Finished -= OnEventSvcProcFinished;

                adminSvcHost.Stop();
                eventSvcHost.Stop();
            }
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        private static void OnAdminSvcProcFinished(object sender, ResultEventArgs e)
        {
            if (e.Exception == null)
                Console.WriteLine(Resources.LogEventServiceAdminStarted);
            else
                Console.WriteLine(Resources.LogEventOnError, e.Exception.ToFullErrorString());
        }

        private static void OnEventSvcProcFinished(object sender, ResultEventArgs e)
        {
            if (e.Exception == null)
                Console.WriteLine(Resources.LogEventServiceEventStarted);
            else
                Console.WriteLine(Resources.LogEventOnError, e.Exception.ToFullErrorString());
        }

        #endregion
    }
}
